package chen.web.util.jdbc.wrapper;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class StatementWrapper implements Statement {

	protected static List<EventHandler> handlers = new ArrayList<EventHandler>();
	
	private Statement stmt;
	protected StatementWrapper(Statement stmt){
		this.stmt = stmt;
	}
	
	protected static void registerHandler(EventHandler listener){
		handlers.add(listener);
	}
	
	protected void fireEvent(Event event){
			for(EventHandler handler : handlers){
				handler.handler(event);
			}
	}
	
	@Override
	public void addBatch(String sql) throws SQLException {
		// TODO Auto-generated method stub
		stmt.addBatch(sql);
	}

	@Override
	public void cancel() throws SQLException {
		// TODO Auto-generated method stub
		stmt.cancel();
	}

	@Override
	public void clearBatch() throws SQLException {
		// TODO Auto-generated method stub
		stmt.clearBatch();
	}

	@Override
	public void clearWarnings() throws SQLException {
		// TODO Auto-generated method stub
		stmt.clearWarnings();
	}

	@Override
	public void close() throws SQLException {
		// TODO Auto-generated method stub
		stmt.close();
	}

	@Override
	public boolean execute(String sql) throws SQLException {
		// TODO Auto-generated method stub
		fireEvent(Event.BeforeExecute);
		boolean result = stmt.execute(sql);
		fireEvent(Event.AfterExecute);
		return result;
	}

	@Override
	public boolean execute(String sql, int autoGeneratedKeys)
			throws SQLException {
		// TODO Auto-generated method stub
		fireEvent(Event.BeforeExecute);
		boolean result =  stmt.execute(sql, autoGeneratedKeys);
		fireEvent(Event.AfterExecute);
		return result;
	}

	@Override
	public boolean execute(String sql, int[] columnIndexes) throws SQLException {
		// TODO Auto-generated method stub
		fireEvent(Event.BeforeExecute);
		boolean result = stmt.execute(sql, columnIndexes);
		fireEvent(Event.AfterExecute);
		return result;
	}

	@Override
	public boolean execute(String sql, String[] columnNames)
			throws SQLException {
		// TODO Auto-generated method stub
		fireEvent(Event.BeforeExecute);
		boolean result = stmt.execute(sql, columnNames);
		fireEvent(Event.AfterExecute);
		return result;
	}

	@Override
	public int[] executeBatch() throws SQLException {
		// TODO Auto-generated method stub
		fireEvent(Event.BeforeExecute);
		int[] result = stmt.executeBatch();
		fireEvent(Event.AfterExecute);
		return result;
	}

	@Override
	public ResultSet executeQuery(String sql) throws SQLException {
		// TODO Auto-generated method stub
		fireEvent(Event.BeforeExecute);
		ResultSet result = stmt.executeQuery(sql);
		fireEvent(Event.AfterExecute);
		return result;
	}

	@Override
	public int executeUpdate(String sql) throws SQLException {
		// TODO Auto-generated method stub
		fireEvent(Event.BeforeExecute);
		int result = stmt.executeUpdate(sql);
		fireEvent(Event.AfterExecute);
		return result;
	}

	@Override
	public int executeUpdate(String sql, int autoGeneratedKeys)
			throws SQLException {
		// TODO Auto-generated method stub
		fireEvent(Event.BeforeExecute);
		int result = stmt.executeUpdate(sql, autoGeneratedKeys);
		fireEvent(Event.AfterExecute);
		return result;
	}

	@Override
	public int executeUpdate(String sql, int[] columnIndexes)
			throws SQLException {
		// TODO Auto-generated method stub
		fireEvent(Event.BeforeExecute);
		int result = stmt.executeUpdate(sql, columnIndexes);
		fireEvent(Event.AfterExecute);
		return result;
	}

	@Override
	public int executeUpdate(String sql, String[] columnNames)
			throws SQLException {
		// TODO Auto-generated method stub
		fireEvent(Event.BeforeExecute);
		int result = stmt.executeUpdate(sql, columnNames);
		fireEvent(Event.AfterExecute);
		return result;
	}

	@Override
	public Connection getConnection() throws SQLException {
		// TODO Auto-generated method stub
		return stmt.getConnection();
	}

	@Override
	public int getFetchDirection() throws SQLException {
		// TODO Auto-generated method stub
		return stmt.getFetchDirection();
	}

	@Override
	public int getFetchSize() throws SQLException {
		// TODO Auto-generated method stub
		return stmt.getFetchSize();
	}

	@Override
	public ResultSet getGeneratedKeys() throws SQLException {
		// TODO Auto-generated method stub
		return stmt.getGeneratedKeys();
	}

	@Override
	public int getMaxFieldSize() throws SQLException {
		// TODO Auto-generated method stub
		return stmt.getMaxFieldSize();
	}

	@Override
	public int getMaxRows() throws SQLException {
		// TODO Auto-generated method stub
		return stmt.getMaxRows();
	}

	@Override
	public boolean getMoreResults() throws SQLException {
		// TODO Auto-generated method stub
		return stmt.getMoreResults();
	}

	@Override
	public boolean getMoreResults(int current) throws SQLException {
		// TODO Auto-generated method stub
		return stmt.getMoreResults();
	}

	@Override
	public int getQueryTimeout() throws SQLException {
		// TODO Auto-generated method stub
		return stmt.getQueryTimeout();
	}

	@Override
	public ResultSet getResultSet() throws SQLException {
		// TODO Auto-generated method stub
		return stmt.getResultSet();
	}

	@Override
	public int getResultSetConcurrency() throws SQLException {
		// TODO Auto-generated method stub
		return stmt.getResultSetConcurrency();
	}

	@Override
	public int getResultSetHoldability() throws SQLException {
		// TODO Auto-generated method stub
		return stmt.getResultSetHoldability();
	}

	@Override
	public int getResultSetType() throws SQLException {
		// TODO Auto-generated method stub
		return stmt.getResultSetType();
	}

	@Override
	public int getUpdateCount() throws SQLException {
		// TODO Auto-generated method stub
		return stmt.getUpdateCount();
	}

	@Override
	public SQLWarning getWarnings() throws SQLException {
		// TODO Auto-generated method stub
		return stmt.getWarnings();
	}

	@Override
	public boolean isClosed() throws SQLException {
		// TODO Auto-generated method stub
		return stmt.isClosed();
	}

	@Override
	public boolean isPoolable() throws SQLException {
		// TODO Auto-generated method stub
		return stmt.isPoolable();
	}

	@Override
	public void setCursorName(String name) throws SQLException {
		// TODO Auto-generated method stub
		stmt.setCursorName(name);
	}

	@Override
	public void setEscapeProcessing(boolean enable) throws SQLException {
		// TODO Auto-generated method stub
		stmt.setEscapeProcessing(enable);
	}

	@Override
	public void setFetchDirection(int direction) throws SQLException {
		// TODO Auto-generated method stub
		stmt.setFetchDirection(direction);
	}

	@Override
	public void setFetchSize(int rows) throws SQLException {
		// TODO Auto-generated method stub
		stmt.setFetchSize(rows);
	}

	@Override
	public void setMaxFieldSize(int max) throws SQLException {
		// TODO Auto-generated method stub
		stmt.setMaxFieldSize(max);
	}

	@Override
	public void setMaxRows(int max) throws SQLException {
		// TODO Auto-generated method stub
		stmt.setMaxRows(max);
	}

	@Override
	public void setPoolable(boolean poolable) throws SQLException {
		// TODO Auto-generated method stub
		stmt.setPoolable(poolable);
	}

	@Override
	public void setQueryTimeout(int seconds) throws SQLException {
		// TODO Auto-generated method stub
		stmt.setQueryTimeout(seconds);
	}

	@Override
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		// TODO Auto-generated method stub
		return stmt.isWrapperFor(iface);
	}

	@Override
	public <T> T unwrap(Class<T> iface) throws SQLException {
		// TODO Auto-generated method stub
		return stmt.unwrap(iface);
	}

}
